import axios from 'axios'

import { Toast } from 'vant'
// import { config } from 'vue/types/umd'

var service = axios.create({
    baseURL:'/api',
    timeout:10000
})

service.interceptors.request.use((config)=>{
    config.headers['Authorization'] = 'Bearer' + ' ' + localStorage.getItem('token');
    Toast.loading({ message: 'loading...', duration: 0 })
    return config;
},(error)=>{
    return Promise.reject(error)
});

service.interceptors.response.use((res)=>{
    Toast.clear(); //隐藏loading
    return res;
},(error)=>{
    Toast.clear(); //隐藏loading
    if (error.response.status == 401) {
        Toast.fail({ message: "身份认证失败,登陆过期,请重新登录!" })
    }
    else if (error.response.status == 404) {
        Toast.fail({ message: "访问路径有误!" })
    }
    else if (error.response.status == 500) {
        Toast.fail({ message: "服务器内部错误!" })
    }
    return Promise.reject(error);
});

export default service;